import time
import uuid
import pymysql
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
db = pymysql.connect(host="121.41.51.49",user="root",password="Aa010903!",database="crmeb1",cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()

# url = "http://121.41.51.49:8899/admin/"

# 作业一：
# 在服务器上自己的crmeb
# 1、登录
# 2、添加用户分组——》删除刚才新增的用户分组
# 3、添加是否成功，断言；删除是否成功，断言
# 组件化，元素与脚本分离


"""driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(7)
driver.find_elements(By.CLASS_NAME,"el-input__inner")[0].send_keys("admin")
driver.find_elements(By.CLASS_NAME,"el-input__inner")[1].send_keys("123456")
driver.find_elements(By.XPATH,"//*[@type='button']")[0].click()
time.sleep(3)
driver.find_elements(By.CLASS_NAME,"font12")[1].click()
# driver.find_element(By.XPATH,"//*[@id='app']/section/div[2]/aside/div[2]/div[1]/div/div/ul/li[2]/span").click()
driver.find_elements(By.CLASS_NAME,"el-menu-item")[1].click()

# todo：
driver.find_element(By.XPATH,"//*[text()='添加分组']").click()
uuid_ = str(uuid.uuid4()).split("-")[0]
time.sleep(3)
driver.find_elements(By.CLASS_NAME,"#")[2].send_keys("丁姐"+uuid_)
time.sleep(3)
driver.find_elements(By.CLASS_NAME,"el-button")[2].click()
time.sleep(5)

text = driver.find_element(By.XPATH,f"//*[text()='丁姐{str(uuid_)}']").text
assert text == "丁姐" + str(uuid_)
print(text)

sql = "select count(*) from eb_user_group"
cursor.execute(sql)
result = cursor.fetchall()
db.close()
for i in result:
    for j in i.values():
        a = int(j)
# m = (a-1)*2
print(a)
m = a*2
print(m)

time.sleep(10)
driver.find_elements(By.XPATH,"//*[text()='删除']")[m-1].click()
# driver.find_element(By.XPATH,"//*[@style='cursor:']").click()
driver.find_elements(By.CLASS_NAME,"el-button")[2].click()

text = driver.find_element(By.XPATH,f"//*[text()='丁姐{str(uuid_)}']").text
assert text == "丁姐" + str(uuid_)
print(text)
# print(a)
# print(result)

time.sleep(3)"""





# 作业二：
# QQ音乐登录：https://y.qq.com/portal/pop_login.html
#
# 作业三-crmeb：
# 1、添加用户标签——》删除刚才新增的用户标签
# 2、添加是否成功，断言；删除是否成功，断言

user_ele = (By.CLASS_NAME,"el-input__inner")     # 输入账号
password_ele = (By.CLASS_NAME,"el-input__inner") # 输入密码
login_ele = (By.XPATH,"//*[@type='button']")     # 登录按钮
user_icon_ele = (By.CLASS_NAME,"font12")         # 用户标签
user_laber_ele = (By.CLASS_NAME,"el-menu-item")  # 用户标签
add_laber_ele = (By.XPATH,"//*[text()='添加标签']")  # 点击添加标签
see_choise_sort_ele = (By.CLASS_NAME,"el-input__inner")       # 点击选择用户标签
choise_sort_ele = (By.CLASS_NAME,"el-select-dropdown__item")  # 选择用户标签[1]
input_laber_ele = (By.CLASS_NAME,"el-input__inner")  # 输入标签
click_accept_ele = (By.CLASS_NAME,"el-button")       # 点击确定
del_lable_ele = (By.XPATH,"//*[text()='删除']")
click_accept1_ele = (By.CLASS_NAME,"el-button")

class CRMEB:
    uuid_ = str(uuid.uuid4()).split('-')[0]
    def __init__(self,driver,url):
        self.driver = driver
        # 初始化驱动
        self.driver = webdriver.Chrome()
        # 打开网页
        self.driver.get(url)
        # 设置隐式等待
        self.driver.implicitly_wait(15)
        # 最大化窗口
        self.driver.maximize_window()
    def login_(self,user:str,password:str):
        # 输入账号：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_ele))[0].send_keys(user)
        # 输入密码：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(password_ele))[1].send_keys(password)
        # 点击登录按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(login_ele))[0].click()
    def add_user_laber(self,name):
        self.name = name
        # 点击用户标签
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_icon_ele))[1].click()
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_laber_ele))[2].click()
        # 点击添加标签
        # By.XPATH,"//*[text()='添加标签']"
        time.sleep(3)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_laber_ele)).click()
        # 选择客户来源
        # By.CLASS_NAME,"el-input__inner"[2]
        time.sleep(3)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(see_choise_sort_ele))[2].click()
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(choise_sort_ele))[5].click()
        # 定位输入框,输入内容
        # By.CLASS_NAME,"el-input__inner"[3]
        time.sleep(3)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(input_laber_ele))[3].send_keys(self.name+str(self.uuid_))
        # 点击确定按钮
        # By.CLASS_NAME,"el-button"[2]
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(click_accept_ele))[2].click()
    def cherk_name(self):
        text = self.driver.find_element(By.XPATH, f"//*[text()='{self.name}{str(self.uuid_)}']").text
        assert text == self.name + str(self.uuid_)
        print(text)
        time.sleep(2)
    def del_label(self):
        # By.XPATH,"//*[text()='删除']"[-1]
        time.sleep(5)
        sql = "select count(*) from eb_user_label"
        cursor.execute(sql)
        result = cursor.fetchall()
        db.close()
        for i in result:
            for j in i.values():
                a = int(j) * 2 - 1
        print(a)
        time.sleep(2)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(del_lable_ele))[a].click()

        # By.CLASS_NAME,"el-button"[2]
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(click_accept1_ele))[2].click()

if __name__ == '__main__':
    driver = webdriver.Chrome()
    url = "http://121.41.51.49:8899/admin/"
    c1 = CRMEB(driver,url)
    c1.login_("admin","123456")
    c1.add_user_laber("小丁姐")
    c1.cherk_name()

    c1.del_label()
    c1.cherk_name()

    time.sleep(2)

